package com.demo.no1springboot.controller;

import com.demo.no1springboot.pojo.Users;
import com.demo.no1springboot.service.UsersService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UsersController {
    @Autowired
    private UsersService usersService;

    @GetMapping("/showPage")
    @ResponseBody
    public Object showPage() {
        List<Users> list = usersService.queryUsers();
        return list;
    }

    @GetMapping("/addPage/{addPage}")
    public String addPage(@PathVariable String addPage) {
        return addPage;
    }

    @PostMapping("/addUser")
    public Object addUser(Users users) {
        System.out.println(users);
        int i=  usersService.addUser(users);
        return "redirect:/queryList";
    }
    @GetMapping("/queryList")
    public String queryUserList(Model model) {
        List<Users> list = usersService.queryUsers();
        model.addAttribute("list",list);
        return "list";
    }
    @GetMapping("/login/{login}")
    public String login(@PathVariable String login) {
        return login;
    }
    @RequestMapping("/login")
    public String login(Model model, Users users, HttpSession session) {
        Users user = usersService.login(users);
        if(user!=null){
            return "redirect:/queryList";
        }
        return "redirect:/addPage/add";
    }
    @RequestMapping("/deleteUser")
    public Integer deleteUser(Integer id){
        return usersService.deleteById(id);
    }
}